//
//  UIBarButtonItem+Extension.swift
//  新浪微博
//
//  Created by dafei on 16/5/21.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit

extension UIBarButtonItem {


    convenience init(imageName: String? = nil, title: String? = nil, target: AnyObject? ,action: Selector) {
        
        self.init()
        
        let btn = UIButton()
        //设置图片
        if let img = imageName {
        
        btn.setImage(UIImage(named: img), forState: .Normal)
        btn.setImage(UIImage(named: img + "_highlighted"), forState: .Highlighted)
            
        }
        btn.addTarget(target, action: action, forControlEvents: .TouchUpInside)
        btn.setTitle(title, forState: .Normal)
        
        //设置图片的显示范围
        btn.imageEdgeInsets = UIEdgeInsetsMake(0, -8, 0, 0)
        
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        
        btn.sizeToFit()
        
        //设置自定义视图
        self.customView = btn
        
    }



}
